package com.xs.ai.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.xs.ai.data.BookingStatus;
import com.xs.ai.services.HealerBookingService;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import redis.clients.jedis.JedisPooled;

import java.time.LocalDate;
import java.util.function.Function;


@Configuration
public class  BookingTools {


	@JsonInclude(Include.NON_NULL)
	public record BookingDetails(String bookingNumber, String name, LocalDate date, BookingStatus bookingStatus,
			String from, String to, String bookingClass) {
	}
	@JsonInclude(JsonInclude.Include.NON_NULL)
	public record Request(
			@JsonProperty(required = true, value = "bookingNumber") @JsonPropertyDescription("预定号, 比如101") String bookingNumber,
			@JsonProperty(required = true, value = "name") @JsonPropertyDescription("用户名, 比如陈京龙") String name) {
	}

	public record Response(String description) {
	}

	@Bean
	@Description("根据用户的预定号和姓名取消订单")
	//不需要注入  但是需要包装请求参数  不知道能不能写成多参数形式
	public Function<Request, Response>  CancelOrder(HealerBookingService healerBookingService){
		return healerBookingService::cancelBooking;
	}
	//配置向量数据库  手动配置的方式  TODO 后续更改为redis 或者mongDB  待删除
	@Bean(name = "customVectorStore") // 重命名Bean
	VectorStore vectorStore(JedisPooled jedisPooled, EmbeddingModel embeddingModel) {
		return RedisVectorStore.builder(jedisPooled, embeddingModel)
				.indexName("default-index")                //todo 暂时没有找到修改标签的方法
				.prefix("medi-prefix")                  // Optional: defaults to "embedding:"
				.metadataFields(                         // Optional: define metadata fields for filtering
						RedisVectorStore.MetadataField.tag("country"),
						RedisVectorStore.MetadataField.numeric("year"))
				.initializeSchema(true)                   // Optional: defaults to false
				.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
				.build();
	}

	@Bean
	public JedisPooled jedisPooled() {
		return new JedisPooled("redis-11793.crce178.ap-east-1-1.ec2.redns.redis-cloud.com", 11793,"default", "8bcXlfvzPC7IVmK9SCIGG1eLyP0jmNTz");
	}

	//配置，模型上下文
	@Bean
	public EmbeddingModel embeddingModel() {
		//
		var dashScopeApi = new DashScopeApi(System.getenv("AI_DASHSCOPE_API_KEY"));
		return new DashScopeEmbeddingModel(dashScopeApi, MetadataMode.EMBED,
				DashScopeEmbeddingOptions.builder()
						.withModel("text-embedding-v2")
						.build());
	}

}
